const R = require('ramda')

/**
 * 出库打印对象
 */
class ProductLeavePrintModel {
  /**
   * 构造函数
   * @param outTime 出库时间
   * @param tickerNo 单号
   * @parm receiveUnit 收货单位
   * @param receiver 收货人
   * @param receiverPhone 收货人手机
   * @param objWithCar 随车附物品
   * @param carNo 车号
   * @param driverName 驾驶员姓名
   * @param driverPhone 驾驶员手机
   * @param freight 运费
   * @param tableListData 出库数据数组 (不同的打印模版需要的数据不同)
   */
  constructor(outTime, tickerNo, receiveUnit, receiver, receiverPhone, objWithCar, carNo, driverName, driverPhone, freight, tableListData) {
    this.outTime = outTime
    this.tickerNo = tickerNo
    this.receiveUnit = receiveUnit
    this.receiver = receiver
    this.receiverPhone = receiverPhone
    this.objWithCar = objWithCar
    this.carNo = carNo
    this.driverName = driverName
    this.driverPhone = driverPhone
    this.tableListData = tableListData
    this.freight = freight

    this.document = null
  }

  addTd(tr, str) {
    const td = this.document.createElement('td')

    td.innerText = str
    tr.appendChild(td)
  }

  common() {
    this.document.querySelector('#receiveUnit').innerHTML = this.receiveUnit.toString()
    this.document.querySelector('#time').innerHTML = this.outTime.toString().substr(0, 11)
    this.document.querySelector('#tickerNo').innerHTML = this.tickerNo.toString()
    this.document.querySelector('#receiver').innerHTML = this.receiver.toString()
    this.document.querySelector('#phone').innerHTML = this.receiverPhone.toString()
    this.document.querySelector('#objWithCar').innerHTML = this.objWithCar.toString()
    this.document.querySelector('#carNo').innerHTML = this.carNo.toString()
    this.document.querySelector('#driverName').innerHTML = this.driverName.toString()
    this.document.querySelector('#driverPhone').innerHTML = this.driverPhone.toString()
    this.document.querySelector('#freight').innerHTML = this.freight.toString()
  }

  print1(winId) {
    this.document = R.prop('document', winId)
    const content = this.document.querySelector('#content')

    this.common()

    content.innerHTML = ''

    let numSum = 0
    let sum = 0

    this.tableListData.forEach((row, index) => {
      const {
        productNo,
        boxSupplyWay,
        boxNumber,
        boxZhiNumber,
        trayNumber,
        trayEachZhiNumber,
        trayLayerNumber,
        remark
      } = row
      const tr = this.document.createElement('tr')

      this.addTd(tr, index + 1)
      this.addTd(tr, productNo)
      if (boxSupplyWay === 0) {
        this.addTd(tr, boxNumber)
        numSum += boxNumber
        this.addTd(tr, boxZhiNumber)
        this.addTd(tr, boxNumber * boxZhiNumber)
        sum += boxNumber * boxZhiNumber
      } else if (boxSupplyWay === 1) {
        this.addTd(tr, trayNumber)
        numSum += trayNumber
        this.addTd(tr, trayEachZhiNumber * trayLayerNumber)
        this.addTd(tr, trayNumber * trayEachZhiNumber * trayLayerNumber)
        sum += trayNumber * trayEachZhiNumber * trayLayerNumber
      } else {
        this.addTd(tr, '')
        this.addTd(tr, '')
        this.addTd(tr, '')
      }
      this.addTd(tr, '')
      this.addTd(tr, remark)
      content.appendChild(tr)
    })

    this.document.querySelector('#numSum').innerHTML = numSum.toString()
    this.document.querySelector('#sum').innerHTML = sum.toString()
  }

  print2(winId) {
    this.document = R.prop('document', winId)
    const content = this.document.querySelector('#content')

    this.common()

    content.innerHTML = ''

    let numSum = 0
    let sum = 0

    this.tableListData.forEach((row, index) => {
      const {
        orderNo,
        codeNo,
        confirmNo,
        productNo,
        boxSupplyWay,
        boxNumber,
        boxZhiNumber,
        trayNumber,
        trayEachZhiNumber,
        trayLayerNumber,
        remark
      } = row
      const tr = this.document.createElement('tr')

      this.addTd(tr, index + 1)
      this.addTd(tr, orderNo)
      this.addTd(tr, codeNo || '')
      this.addTd(tr, confirmNo || '')
      this.addTd(tr, productNo)
      if (boxSupplyWay === 0) {
        this.addTd(tr, boxNumber)
        numSum += boxNumber
        this.addTd(tr, boxZhiNumber)
        this.addTd(tr, boxNumber * boxZhiNumber)
        sum += boxNumber * boxZhiNumber
      } else if (boxSupplyWay === 1) {
        this.addTd(tr, trayNumber)
        numSum += trayNumber
        this.addTd(tr, trayEachZhiNumber * trayLayerNumber)
        this.addTd(tr, trayNumber * trayEachZhiNumber * trayLayerNumber)
        sum += trayNumber * trayEachZhiNumber * trayLayerNumber
      } else {
        this.addTd(tr, '')
        this.addTd(tr, '')
        this.addTd(tr, '')
      }
      this.addTd(tr, remark)
      content.appendChild(tr)
    })

    this.document.querySelector('#numSum').innerHTML = numSum.toString()
    this.document.querySelector('#sum').innerHTML = sum.toString()
  }

  print3(winId) {
    this.document = R.prop('document', winId)
    const content = this.document.querySelector('#content')

    this.common()

    content.innerHTML = ''

    let numSum = 0
    let sum = 0

    this.tableListData.forEach((row, index) => {
      const {
        orderNo,
        productNo,
        boxSupplyWay,
        boxNumber,
        boxZhiNumber,
        trayNumber,
        trayEachZhiNumber,
        trayLayerNumber,
        remark
      } = row
      const tr = this.document.createElement('tr')

      this.addTd(tr, index + 1)
      this.addTd(tr, orderNo)
      this.addTd(tr, productNo)
      this.addTd(tr, productNo)
      if (boxSupplyWay === 0) {
        this.addTd(tr, boxNumber)
        numSum += boxNumber
        this.addTd(tr, boxZhiNumber)
        this.addTd(tr, boxNumber * boxZhiNumber)
        sum += boxNumber * boxZhiNumber
      } else if (boxSupplyWay === 1) {
        this.addTd(tr, trayNumber)
        numSum += trayNumber
        this.addTd(tr, trayEachZhiNumber * trayLayerNumber)
        this.addTd(tr, trayNumber * trayEachZhiNumber * trayLayerNumber)
        sum += trayNumber * trayEachZhiNumber * trayLayerNumber
      } else {
        this.addTd(tr, '')
        this.addTd(tr, '')
        this.addTd(tr, '')
      }
      this.addTd(tr, remark)
      content.appendChild(tr)
    })

    this.document.querySelector('#numSum').innerHTML = numSum.toString()
    this.document.querySelector('#sum').innerHTML = sum.toString()
  }
}

export {ProductLeavePrintModel}
